﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using Assert = NUnit.Framework.Assert;
using Description = NUnit.Framework.DescriptionAttribute;
using TestContext = Microsoft.VisualStudio.TestTools.UnitTesting.TestContext;
using CsQuery.Utility;

namespace CsQuery.Tests.Core.Dom
{
    [TestFixture, TestClass]
    public class InnerText: CsQueryTest
    {

        string testHtml = @"<div><span><!-- a comment --></span>
    <script type=text/javascript>var a;</script>
<textarea>textarea contents</textarea>
<![CDATA[ a cdata node]]>
This text has <a href>a link</a></div><div>a new block</div>";


        [Test, TestMethod]
        public void InnerTextGet()
        {
            

            var doc = TestDom("TestHtml");

            // Note: this is what Chrome actually produces. The specific logic that decides how many spaces & line feeds to add seems
            // a bit obtuse and not part of any spec anyway so CsQuery doesn't exactly match it yet.
            
//            var expected = @"Hello there
//
//▾ jamietre  3,215 2 13     |  
//Nested non-hidden span
//Nested non=hidden header";

            var expected = @"Hello there
▾ jamietre 3,215 2 13 |
Nested hidden span
Nested hidden header
Nested non-hidden span
Nested non=hidden header
".NormalizeLineEndings();

            Assert.AreEqual(expected, doc["body"][0].InnerText.NormalizeLineEndings());
        }

        /// <summary>
        /// Compare our TextContent function with the output from Chrome
        /// </summary>

        [Test, TestMethod]
        public void TextContentGet()
        {

            var doc = TestDom("TestHtml");
            var text = doc["body"][0].TextContent.NormalizeLineEndings();
            var expected = testHtmlTextContent.NormalizeLineEndings();

            Assert.AreEqual(expected, text);

            doc = TestDom("jquery-unit-index");
            text = doc["body"][0].TextContent.NormalizeLineEndings();
            expected = jqueryUnitIndexTextContent.NormalizeLineEndings();
            
            Assert.AreEqual(expected, text);

        }
        [Test, TestMethod]
        public void TextContentGet2()
        {
            CQ doc = CQ.CreateDocument(testHtml);

            var text = doc["body"][0].TextContent;

        }

        [Test, TestMethod]
        public void InnerTextGet2()
        {


            CQ doc = CQ.CreateDocument(testHtml);

            var text = doc["body"][0].InnerText;

            Assert.AreEqual("This text has a link" + Environment.NewLine + "a new block", text);


        }
        [Test, TestMethod]
        public void InnerTextSet()
        {
            var doc = TestDom("TestHtml");

            var res = doc["#reputation_link"][0];
            res.InnerText = "change the text";
            Assert.AreEqual(1,res.ChildNodes.Length);
            Assert.AreEqual("change the text", res.InnerText);

        }

        // from chrome
        // var content = document.body.textContent; 
        // var output = "";
        // for (var i=0;i<content.length;i++) {
        //     output+=(output==""?"":",")+content.charCodeAt(i);
        // }

        string testHtmlTextContent = new ushort[] {
            10,32,32,32,32,72,101,108,108,111,32,116,104,101,114,101,10,32,32,32,32,32,10,32,32,32,32,10,32,32,32,32,32,32,32,32,9662,10,32,32,32,32,32,32,32,32,106,97,109,105,101,116,114,101,160,10,32,32,32,32,32,32,32,32,10,32,32,32,32,32,32,32,32,32,32,32,32,51,44,50,49,53,10,32,32,32,32,32,32,32,32,10,32,32,32,32,32,32,32,32,10,32,32,32,32,32,32,32,32,32,32,32,32,10,32,32,32,32,32,32,32,32,32,32,32,32,50,10,32,32,32,32,32,32,32,32,10,32,32,32,32,32,32,32,32,10,32,32,32,32,32,32,32,32,32,32,32,32,10,32,32,32,32,32,32,32,32,32,32,32,32,49,51,10,32,32,32,32,32,32,32,32,10,10,32,32,32,32,32,32,32,32,10,32,32,32,32,32,32,32,32,10,32,32,32,32,32,32,32,32,124,10,32,32,32,32,32,32,32,32,84,101,115,116,32,116,101,120,116,97,114,101,97,32,60,100,105,118,62,60,115,112,97,110,62,60,47,100,105,118,62,10,32,32,32,32,10,10,32,32,32,32,10,32,32,32,32,32,32,32,32,10,32,32,32,32,32,32,32,32,32,32,32,32,10,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,78,101,115,116,101,100,32,104,105,100,100,101,110,32,115,112,97,110,10,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,78,101,115,116,101,100,32,104,105,100,100,101,110,32,104,101,97,100,101,114,10,32,32,32,32,32,32,32,32,32,32,32,32,10,32,32,32,32,32,32,32,32,10,32,32,32,32,32,32,32,32,10,32,32,32,32,32,32,32,32,32,32,32,32,78,101,115,116,101,100,32,110,111,110,45,104,105,100,100,101,110,32,115,112,97,110,10,32,32,32,32,32,32,32,32,32,32,32,32,78,101,115,116,101,100,32,110,111,110,61,104,105,100,100,101,110,32,104,101,97,100,101,114,10,32,32,32,32,32,32,32,32,10,32,32,32,32,10,10
        }.AsString();

        string jqueryUnitIndexTextContent = new ushort[] {
            10,9,106,81,117,101,114,121,32,84,101,115,116,32,83,117,105,116,101,10,9,9,40,109,105,110,105,102,105,101,100,41,10,9,10,9,10,9,10,9,10,9,10,10,9,10,9,10,9,9,10,9,10,9,10,9,10,32,32,32,32,10,9,10,9,10,9,9,83,101,101,32,116,104,105,115,32,98,108,111,103,32,101,110,116,114,121,32,102,111,114,32,109,111,114,101,32,105,110,102,111,114,109,97,116,105,111,110,46,10,9,9,10,9,9,9,72,101,114,101,32,97,114,101,32,115,111,109,101,32,108,105,110,107,115,32,105,110,32,97,32,110,111,114,109,97,108,32,112,97,114,97,103,114,97,112,104,58,32,71,111,111,103,108,101,44,10,9,9,9,71,111,111,103,108,101,32,71,114,111,117,112,115,32,40,76,105,110,107,41,46,10,9,9,9,84,104,105,115,32,108,105,110,107,32,104,97,115,32,99,108,97,115,115,61,34,98,108,111,103,34,58,10,9,9,9,100,105,118,101,105,110,116,111,109,97,114,107,10,10,9,9,10,9,9,10,9,9,9,69,118,101,114,121,116,104,105,110,103,32,105,110,115,105,100,101,32,116,104,101,32,114,101,100,32,98,111,114,100,101,114,32,105,115,32,105,110,115,105,100,101,32,97,32,100,105,118,32,119,105,116,104,32,105,100,61,34,102,111,111,34,46,10,9,9,9,84,104,105,115,32,105,115,32,97,32,110,111,114,109,97,108,32,108,105,110,107,58,32,89,97,104,111,111,10,9,9,9,84,104,105,115,32,108,105,110,107,32,104,97,115,32,99,108,97,115,115,61,34,98,108,111,103,34,58,32,83,105,109,111,110,32,87,105,108,108,105,115,111,110,39,115,32,87,101,98,108,111,103,10,10,9,9,10,9,9,10,9,9,84,114,121,32,116,104,101,109,32,111,117,116,58,10,9,9,10,9,9,10,9,9,10,9,9,9,65,99,116,105,111,110,58,10,9,9,9,10,9,9,9,10,9,9,9,10,10,9,9,9,10,9,9,9,10,9,9,9,10,10,9,9,9,10,9,9,9,10,10,9,9,9,10,9,9,9,10,10,9,9,9,66,117,116,116,111,110,10,10,9,9,9,102,111,111,98,97,114,10,10,9,9,9,10,9,9,9,9,78,111,116,104,105,110,103,10,9,9,9,9,49,10,9,9,9,9,50,10,9,9,9,9,51,10,9,9,9,10,9,9,9,10,9,9,9,9,78,111,116,104,105,110,103,10,9,9,9,9,49,10,9,9,9,9,50,10,9,9,9,9,51,10,9,9,9,10,9,9,9,10,9,9,9,9,78,111,116,104,105,110,103,10,9,9,9,9,49,10,9,9,9,9,50,10,9,9,9,9,51,10,9,9,9,9,110,111,32,118,97,108,117,101,10,9,9,9,10,9,9,9,10,9,9,9,9,10,9,9,9,9,9,78,111,116,104,105,110,103,10,9,9,9,9,9,49,10,9,9,9,9,9,50,10,9,9,9,9,10,9,9,9,9,51,10,9,9,9,9,110,111,32,118,97,108,117,101,10,9,9,9,10,9,9,9,10,9,9,9,9,49,10,9,9,9,9,50,10,9,9,9,9,51,10,9,9,9,10,10,9,9,9,10,9,9,9,9,10,9,9,9,9,10,9,9,9,10,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,10,10,9,9,9,116,101,115,116,32,101,108,101,109,101,110,116,10,9,9,10,9,9,70,108,111,97,116,32,116,101,115,116,46,10,9,9,10,9,9,10,9,9,9,10,9,9,9,10,9,9,10,9,9,10,10,9,9,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,10,10,9,9,10,9,9,9,63,10,90,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,9,65,66,67,10,9,9,9,9,65,66,67,10,9,9,9,9,65,66,67,10,9,9,9,10,9,9,9,10,9,9,9,9,65,66,67,10,9,9,9,9,65,66,67,10,9,9,9,9,65,66,67,10,9,9,9,10,9,9,9,10,9,9,9,9,89,69,83,10,9,9,9,10,9,9,9,10,9,9,9,9,78,79,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,78,79,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,9,78,79,10,9,9,9,10,9,9,9,10,9,9,9,10,9,9,9,9,10,9,9,9,10,9,9,10,9,9,10,9,9,9,10,9,9,9,9,10,9,9,9,9,9,10,9,9,9,9,9,10,9,9,9,9,9,10,9,9,9,9,9,10,9,9,9,9,10,9,9,9,10,9,9,9,104,105,32,116,104,101,114,101,32,10,9,9,9,10,9,9,9,9,104,105,100,100,101,110,10,9,9,9,10,9,9,10,10,9,9,10,9,9,9,10,9,9,9,9,82,105,99,101,10,9,9,9,9,66,101,97,110,115,10,9,9,9,9,66,108,105,110,105,115,10,9,9,9,9,84,111,102,117,10,9,9,9,10,10,9,9,9,73,39,109,32,104,117,110,103,114,121,46,32,73,32,115,104,111,117,108,100,46,46,46,10,9,9,9,46,46,46,69,97,116,32,108,111,116,115,32,111,102,32,102,111,111,100,46,46,46,32,124,10,9,9,9,46,46,46,69,97,116,32,97,32,108,105,116,116,108,101,32,102,111,111,100,46,46,46,32,124,10,9,9,9,46,46,46,69,97,116,32,110,111,32,102,111,111,100,46,46,46,10,9,9,9,46,46,46,69,97,116,32,97,32,98,117,114,103,101,114,46,46,46,10,9,9,9,46,46,46,69,97,116,32,115,111,109,101,32,102,117,110,121,117,110,115,46,46,46,10,9,9,9,46,46,46,69,97,116,32,115,111,109,101,32,102,117,110,121,117,110,115,46,46,46,10,9,9,10,10,9,9,10,9,9,9,10,9,9,9,10,9,9,10,10,9,9,10,9,9,9,49,10,9,9,9,50,10,9,9,10,9,10,9,10,9,10,9,9,10,9,9,9,102,97,100,101,73,110,102,97,100,101,73,110,10,9,9,9,102,97,100,101,79,117,116,102,97,100,101,79,117,116,10,10,9,9,9,115,104,111,119,115,104,111,119,10,9,9,9,104,105,100,101,104,105,100,101,10,10,9,9,9,116,111,103,103,108,101,105,110,116,111,103,103,108,101,105,110,10,9,9,9,116,111,103,103,108,101,111,117,116,116,111,103,103,108,101,111,117,116,10,10,10,9,9,9,115,108,105,100,101,85,112,115,108,105,100,101,85,112,10,9,9,9,115,108,105,100,101,68,111,119,110,115,108,105,100,101,68,111,119,110,10,10,9,9,9,115,108,105,100,101,84,111,103,103,108,101,73,110,115,108,105,100,101,84,111,103,103,108,101,73,110,10,9,9,9,115,108,105,100,101,84,111,103,103,108,101,79,117,116,115,108,105,100,101,84,111,103,103,108,101,79,117,116,10,10,9,9,9,102,97,100,101,84,111,103,103,108,101,73,110,102,97,100,101,84,111,103,103,108,101,73,110,10,9,9,9,102,97,100,101,84,111,103,103,108,101,79,117,116,102,97,100,101,84,111,103,103,108,101,79,117,116,10,10,9,9,9,102,97,100,101,84,111,102,97,100,101,84,111,10,9,9,10,10,9,9,10,9,10,10
        }.AsString();


    }
}

